package com.team6.cet.controller;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.team6.cet.entity.Admin;
import com.team6.cet.entity.IStatusMessage;
import com.team6.cet.entity.OrdinaryUser;
import com.team6.cet.entity.ResponseResult;
import com.team6.cet.service.AdminService;

import io.swagger.annotations.Api; 
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
 
@Api(tags = "/管理员")
@RestController
@Validated
@RequestMapping("/Admin")
public class AdminController {

	@Autowired
	public AdminService adminService;
	
	@ApiOperation(value="删除记录")
	@DeleteMapping(value="/del")
	public int delById(
	@ApiParam(required=true,value="查询记录编号") @RequestBody Integer id) {
		return adminService.delById(id);
	}

	
	@ApiOperation(value = "根据ID查询记录") 
	@PutMapping(value = "/get")
	public Admin getById(
			@ApiParam(required = true, value = "查询记录编号")
			@RequestBody @Valid Integer id) {
		return adminService.getById(id);
	}

	@ApiParam(required = true , value = "记录内容")
	@PutMapping(value = "/edit") 
	public int editById(@Valid @RequestBody Admin record) {
		return adminService.editById(record);
		 
	}
	
	@ApiOperation(value="删除记录")
	@PostMapping(value="/add")
	public ResponseResult add(@ApiParam(required = true, value = "添加考试信息")@RequestBody @Valid  Admin record) {
		
		adminService.save(record);
	 
		ResponseResult responseResult=new ResponseResult();
		responseResult.setCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
		responseResult.setMessage("添加成功");
		responseResult.setObj(record);
		return responseResult;
	}
	
	
	// 登录页面
		@PostMapping(value = "/login")
		public ResponseResult login(@ApiParam(required = true, value = "添加考试信息")@RequestBody @Valid Admin user) {

			ResponseResult responseResult = new ResponseResult();
			responseResult.setCode(IStatusMessage.SystemStatus.ERROR.getCode());
			if (null == user) {
				responseResult.setCode(IStatusMessage.SystemStatus.PARAM_ERROR.getCode());
				responseResult.setMessage("请求参数有误，请您稍后再试");
			 
				return responseResult;
			} 
			else{
				boolean flag = false;
					Admin cuser= adminService.getnamepassword(user.getName(), user.getPassword());
				 
					if (cuser!=null) {
						/* 获得请求后的状态码 */
						responseResult.setCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
						responseResult.setObj(cuser);
					}
					else {
						/* 获得请求后的状态码 */
						responseResult.setCode(IStatusMessage.SystemStatus.ERROR.getCode());
						responseResult.setMessage("密码错误！");
					}
					responseResult.setObj(cuser);
				return responseResult;
			}
		}

	 

}
